using AutoMapper;
using B.S.XZYData.Api.Read.Dto.Plan;
using B.S.XZYData.Domain.LJC;

namespace B.S.XZYData.Api.Read.Profiles
{
    /// <summary>
    /// 生产计划AutoMapper映射配置
    /// </summary>
    public class PlanProfile : Profile
    {
        public PlanProfile()
        {
            // PlanModel -> PlanDto
            CreateMap<PlanModel, PlanDto>();

            // PlanModel -> PlanDetailDto
            CreateMap<PlanModel, PlanDetailDto>();

            // PlanItemModel -> PlanItemDto (保留原有映射)
            CreateMap<PlanItemModel, PlanItemDto>();

            // ProductModel -> PlanItemDto (新增：从产品主表映射到计划明细DTO)
            CreateMap<ProductModel, PlanItemDto>()
                .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Id))
                .ForMember(dest => dest.PlanId, opt => opt.MapFrom(src => src.PlanId ?? 0))
                .ForMember(dest => dest.ProductCode, opt => opt.MapFrom(src => src.Code))
                .ForMember(dest => dest.ProductName, opt => opt.MapFrom(src => src.Name))
                .ForMember(dest => dest.Spec, opt => opt.MapFrom(src => src.Spec))
                .ForMember(dest => dest.Quantity, opt => opt.MapFrom(src => 0)) // 设置默认值0
                .ForMember(dest => dest.DoneQuantity, opt => opt.MapFrom(src => (int?)null)) // 设置为null
                .ForMember(dest => dest.WorkOrderCode, opt => opt.MapFrom(src => (string?)null)) // 设置为null
                .ForMember(dest => dest.CreateTime, opt => opt.MapFrom(src => src.CreateTime))
                .ForMember(dest => dest.UpdateTime, opt => opt.MapFrom(src => src.UpdateTime));

            // ProductModel -> PlanProductDto (新增：产品主表到计划产品DTO的映射)
            CreateMap<ProductModel, PlanProductDto>()
                .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Id))
                .ForMember(dest => dest.PlanId, opt => opt.MapFrom(src => src.PlanId))
                .ForMember(dest => dest.ProductCode, opt => opt.MapFrom(src => src.Code))
                .ForMember(dest => dest.ProductName, opt => opt.MapFrom(src => src.Name))
                .ForMember(dest => dest.Spec, opt => opt.MapFrom(src => src.Spec))
                .ForMember(dest => dest.Unit, opt => opt.MapFrom(src => src.Unit))
                .ForMember(dest => dest.Producer, opt => opt.MapFrom(src => src.Producer))
                .ForMember(dest => dest.ProductionStartTime, opt => opt.MapFrom(src => src.ProductionStartTime))
                .ForMember(dest => dest.ProductionEndTime, opt => opt.MapFrom(src => src.ProductionEndTime))
                .ForMember(dest => dest.CreateTime, opt => opt.MapFrom(src => src.CreateTime))
                .ForMember(dest => dest.UpdateTime, opt => opt.MapFrom(src => src.UpdateTime));

            // ProductModel -> ProductSelectDto
            CreateMap<ProductModel, ProductSelectDto>()
                .ForMember(dest => dest.Code, opt => opt.MapFrom(src => src.Code))
                .ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.Name))
                .ForMember(dest => dest.Spec, opt => opt.MapFrom(src => src.Spec))
                .ForMember(dest => dest.Location, opt => opt.Ignore()); // ProductModel中没有库位字段，暂时忽略
        }
    }
} 